// pages/management/bill/bill.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 账单展示列表
    billList: [],
    showPriceInput: false,
    title: ""
  },
  addAccountBook() {
    let params = {
      title: this.data.title
    };
    wx.$apis.addAccountBook(params).then((res) => {
      // 重置
      this.setData({
        title: ""
      });
      this.onCancel();
      this.getData();
      wx.showToast({
        title: "添加成功",
        icon: "success"
      });
    });
  },
  // 账单填写
  toBillFill(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/management/bill-fill/index?id=" + id
    });
  },
  // 提交账本名称
  onSubmit() {
    this.addAccountBook();
  },
  // 添加账本
  toAddBill() {
    this.setData({
      showPriceInput: true
    });
  },
  onCancel() {
    this.setData({
      showPriceInput: false
    });
  },
  getData() {
    let params = {
      asc: true,
      columns: [],
      current: 1,
      currentMongo: 0,
      size: 200
    };
    wx.showLoading({
      title: "查询中..."
    });
    wx.$apis.getAccountBook(params).then((res) => {
      if (res.data) {
        const { records } = res.data;
        this.setData({
          billList: records.map((item) => {
            // 收入 + 未结 - 支出 = 合计利润，不管是否为负数
            let incomeTotal = Math.round(((item.incomeAmount * 1 + item.unpaidAmount * 1 - item.paidAmount) / 100) * 100) / 100;
            return {
              ...item,
              incomeAmount: Math.round((item.incomeAmount / 100) * 100) / 100,
              unpaidAmount: Math.round((item.unpaidAmount / 100) * 100) / 100,
              paidAmount: Math.round((item.paidAmount / 100) * 100) / 100,
              incomeTotal,
              createTime: wx.$dayjs(item.createTime).format("YYYY-MM-DD HH:mm:ss")
            };
          })
        });
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  onShow() {
    this.getData();
  }
});
